﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Principal;
using WindowsFormsApplication2.Properties;
using System.Threading;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        private Random m_Engine = new Random();
        private delegate void AddControlHandler(Control ctl);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //WindowsIdentity identity = WindowsIdentity.GetCurrent();
            //IdentityReferenceCollection groupsSIDs = identity.Groups;

            //foreach (IdentityReference identityReference in groupsSIDs)
            //{
            //    SecurityIdentifier sid = new SecurityIdentifier(identityReference.Value);
            //    Console.WriteLine((sid.Translate(typeof(NTAccount)) as NTAccount).Value);
            //}

            this.backgroundWorker1.RunWorkerAsync();

        }

        private void CreatePlot()
        {
            int dimensionX = this.pictureBox1.Width;
            int dimensionY = this.pictureBox1.Height;
            int population = m_Engine.Next(1200);

            double circleDegs = 360d;

            Bitmap bitmap = new Bitmap(dimensionX, dimensionY);
            Bitmap flag = Resources.Error;
            Graphics plot = Graphics.FromImage(bitmap);
            plot.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            plot.Clear(Color.White);

            float posX = dimensionX / 2;
            float posY = dimensionY / 2;

            double radius = 45;
            double stepAngle = 20d;
            double currentAngle = 0d;
            double offset = 5d;

            for (int itemIdx = 0; itemIdx < population - 1; itemIdx++)
            {
                float stepX = (float)(radius * Math.Cos(currentAngle));
                float stepY = -(float)(radius * Math.Sin(currentAngle));

                float currentX = posX + stepX;
                float currentY = posY + stepY;

                plot.DrawImage(flag, currentX - flag.Width / 2, currentY - flag.Height / 2);

                plot.DrawString(
                    String.Format("Angle:{0}", currentAngle.ToDeg().ToString()),
                    new Font("Tahoma", 7f), Brushes.Black,
                    (float)(currentX + offset * 3),
                    (float)(currentY - offset * 2));

                plot.DrawLine(new Pen(Brushes.Blue), new Point((int)currentX, (int)currentY), new Point((int)posX, (int)posY));

                this.pictureBox1.Image = bitmap;
                Thread.Sleep(100);

                if (currentAngle > circleDegs.ToRad())
                {
                    currentAngle = 0d;
                    radius += radius * 2 / 3;
                }
                else
                {
                    currentAngle += stepAngle.ToRad();
                }
            }
            
        }

        private void AddControl(Control ctl)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new AddControlHandler(AddControl), new object[] { ctl });
                return;
            }
            
            this.Controls.Add(ctl);
            ctl.BringToFront();
        }

        private void CreatePlotWithControls()
        {
            int dimensionX = this.Width;
            int dimensionY = this.Height;
            int population = m_Engine.Next(1200);
            
            double circleDegs = 360d;

            //Bitmap bitmap = new Bitmap(dimensionX, dimensionY);
            Bitmap flag = Resources.Error;
            //Graphics plot = Graphics.FromImage(bitmap);
            //plot.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            //plot.Clear(Color.White);
            

            float posX = dimensionX / 2;
            float posY = dimensionY / 2;

            double radius = 45;
            double stepAngle = 20d;
            double currentAngle = 0d;
            double offset = 5d;

            for (int itemIdx = 0; itemIdx < population - 1; itemIdx++)
            {
                float stepX = (float)(radius * Math.Cos(currentAngle));
                float stepY = -(float)(radius * Math.Sin(currentAngle));

                float currentX = posX + stepX;
                float currentY = posY + stepY;

                //plot.DrawImage(flag, currentX - flag.Width / 2, currentY - flag.Height / 2);

                //plot.DrawString(
                //    String.Format("Angle:{0}", currentAngle.ToDeg().ToString()),
                //    new Font("Tahoma", 7f), Brushes.Black,
                //    (float)(currentX + offset * 3),
                //    (float)(currentY - offset * 2));

                //plot.DrawLine(new Pen(Brushes.Blue), new Point((int)currentX, (int)currentY), new Point((int)posX, (int)posY));

                PictureBox picBox = new PictureBox();
                picBox.Width = flag.Width;
                picBox.Height = flag.Height;
                picBox.Image = flag;
                picBox.Tag = itemIdx;
                picBox.Top = (int)currentY;
                picBox.Left = (int)currentX;
                picBox.Click += new EventHandler(picBox_Click);
                picBox.MouseUp += new MouseEventHandler(picBox_MouseUp);

                AddControl(picBox);

                Thread.Sleep(100);

                if (currentAngle > circleDegs.ToRad())
                {
                    currentAngle = 0d;
                    radius += radius * 2 / 3;
                }
                else
                {
                    currentAngle += stepAngle.ToRad();
                }
            }
        }

        void picBox_MouseUp(object sender, MouseEventArgs e)
        {
            //PictureBox picBox = sender as PictureBox;
            //picBox.Left = e.X;
            //picBox.Top = e.Y;
        }

        void picBox_Click(object sender, EventArgs e)
        {
            PictureBox picBox = sender as PictureBox;
            MessageBox.Show(picBox.Tag.ToString());
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            CreatePlotWithControls();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.backgroundWorker1.CancelAsync();
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            PictureBox picBox = sender as PictureBox;
            picBox.Left = e.X;
            picBox.Top = e.Y;
        }

        
    }
}
